package br.edu.universo.si.projeto2.infojob.client.apresentacao;

import br.edu.universo.si.projeto2.infojob.client.bean.BeanEmpresaAssinante;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanEstado;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanTitularidade;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanVaga;
import br.edu.universo.si.projeto2.infojob.client.componentes.NumberBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.comboboxes.EstadosListBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.comboboxes.TitularidadesListBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.ErrorDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.SuccessDialogBox;
import br.edu.universo.si.projeto2.infojob.client.util.Util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ACadastroVagas extends DockPanel{
	
	private FlexTable flexTable;
	
	private HorizontalPanel botoesPanel;
	
	private Button salvarBtn;
	private Button limparBtn;

	private TextBox cargoTxt;
	private ListBox cargaHorariaSemanalCb;
	private NumberBox remuneracaoCb;
	private ListBox titularidadeCb;
	private TextArea atribuicoesTxt;
	private TextBox cidadeTxt;
	private EstadosListBox estadosCb;
	private NumberBox experienciaTxt;
	private TextArea requisitosTxt;
	
	private BeanEmpresaAssinante bEmpresaAssinante;
	
	public ACadastroVagas(BeanEmpresaAssinante bEmpresaAssinante)
	{
		this.bEmpresaAssinante = bEmpresaAssinante;

		inicializarComponentes();

		flexTable.setCellSpacing(5);

		// seta os tamanhos dos campos...
		cargoTxt.setWidth("150px");
		remuneracaoCb.setWidth("100px");
		atribuicoesTxt.setSize("300px", "150px");
		cidadeTxt.setWidth("100px");
		experienciaTxt.setWidth("50px");
		requisitosTxt.setSize("300px", "150px");

		flexTable.setWidget(0, 0, new HTML("<b>OFERTAR VAGA</b>"));
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);

		flexTable.setWidget(1, 0, new HTML("Cargo: "));
		flexTable.setWidget(1, 1, cargoTxt);
		flexTable.getFlexCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(2, 0, new HTML("Carga Horária Semanal: "));
		flexTable.setWidget(2, 1, cargaHorariaSemanalCb);
		flexTable.getFlexCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(3, 0, new HTML("Remuneração: "));
		flexTable.setWidget(3, 1, remuneracaoCb);
		flexTable.getFlexCellFormatter().setAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(4, 0, new HTML("Titularidade: "));
		flexTable.setWidget(4, 1, titularidadeCb);
		flexTable.getFlexCellFormatter().setAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(5, 0, new HTML("Atribuições: "));
		flexTable.setWidget(5, 1, atribuicoesTxt);
		flexTable.getFlexCellFormatter().setAlignment(5, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		flexTable.setWidget(6, 0, new HTML("Cidade: "));
		flexTable.setWidget(6, 1, cidadeTxt);
		flexTable.getFlexCellFormatter().setAlignment(6, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(7, 0, new HTML("Estados: "));
		flexTable.setWidget(7, 1, estadosCb);
		flexTable.getFlexCellFormatter().setAlignment(7, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(8, 0, new HTML("Experiência: "));
		flexTable.setWidget(8, 1, experienciaTxt);
		flexTable.getFlexCellFormatter().setAlignment(8, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(9, 0, new HTML("Requisitos: "));
		flexTable.setWidget(9, 1, requisitosTxt);
		flexTable.getFlexCellFormatter().setAlignment(9, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		botoesPanel.add(salvarBtn);
		botoesPanel.add(limparBtn);

		flexTable.setWidget(10, 0, botoesPanel);
		flexTable.getFlexCellFormatter().setColSpan(10, 0, 2);
		flexTable.getFlexCellFormatter().setAlignment(10, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		ACadastroVagas.this.add(flexTable, DockPanel.CENTER);		

		salvarBtn.addClickListener(
				new ClickListener()
				{
					public void onClick(Widget sender)
					{
						BeanVaga bVaga = new BeanVaga();

						bVaga.setBEmpresaAssinante(ACadastroVagas.this.bEmpresaAssinante);

						bVaga.setCargo(cargoTxt.getText());
						bVaga.setCargaHorariaSemanal(Integer.parseInt(cargaHorariaSemanalCb.getValue(cargaHorariaSemanalCb.getSelectedIndex())));
						bVaga.setRemuneracao(Double.parseDouble(cargaHorariaSemanalCb.getValue(cargaHorariaSemanalCb.getSelectedIndex())));

						BeanTitularidade bTitularidade = new BeanTitularidade();
						bTitularidade.setId(Integer.parseInt(titularidadeCb.getValue(titularidadeCb.getSelectedIndex())));
						bVaga.setBTitularidade(bTitularidade);

						bVaga.setAtribuicoes(atribuicoesTxt.getText());
						bVaga.setCidade(cidadeTxt.getText());

						BeanEstado bEstado = new BeanEstado();
						bEstado.setId(Integer.parseInt(estadosCb.getValue(estadosCb.getSelectedIndex())));
						bVaga.setBEstado(bEstado);

						bVaga.setExperiencia(Integer.parseInt(experienciaTxt.getText()));
						bVaga.setRequisitos(requisitosTxt.getText());
						bVaga.setEncerrada(false);

						AsyncCallback<Object> cbCadastrarVaga = new AsyncCallback<Object>()
						{
							public void onSuccess(Object result)
							{
								SuccessDialogBox successDialobBox = new SuccessDialogBox("Vaga cadastrada com sucesso");
								successDialobBox.center();
								successDialobBox.show();
								limparCampos();
							};

							public void onFailure(Throwable caught)
							{
								ErrorDialogBox errorDialogBox = new ErrorDialogBox("Erro ao cadastrar a vaga: " + caught.getMessage());
								errorDialogBox.center();
								errorDialogBox.show();
							};
						};

						if (verificarCampos())
						{
							Util.getService().cadastrarVaga(bVaga, cbCadastrarVaga);
						}
					}
				}
		);
		
		limparBtn.addClickListener(
				new ClickListener()
				{
					public void onClick(Widget sender)
					{
						limparCampos();
					}
				}
		);
	}

	
	private void inicializarComponentes()
	{
		flexTable = new FlexTable();

		botoesPanel = new HorizontalPanel();
		botoesPanel.setSpacing(3);

		salvarBtn = new Button("Salvar");
		limparBtn = new Button("Limpar");

		cargoTxt = new TextBox();
		cidadeTxt = new TextBox();
		estadosCb = new EstadosListBox();
		
		cargaHorariaSemanalCb = new ListBox(false);
		remuneracaoCb = new NumberBox();

		// coloca os valores pra carga horaria semanal
		cargaHorariaSemanalCb.addItem("", "");
		cargaHorariaSemanalCb.addItem("20 horas", "20");
		cargaHorariaSemanalCb.addItem("30 horas", "30");
		cargaHorariaSemanalCb.addItem("35 horas", "35");
		cargaHorariaSemanalCb.addItem("40 horas", "40");

		
		titularidadeCb = new TitularidadesListBox();
		atribuicoesTxt = new TextArea();
		cidadeTxt = new TextBox();
		estadosCb.setItemSelected(0, true);
		experienciaTxt = new NumberBox();
		requisitosTxt = new TextArea();
	}

	private boolean verificarCampos()
	{
		if (cargoTxt.getText().equalsIgnoreCase(""))
		{
			return false;
		}

		return true;
	}

	private void limparCampos()
	{
		cargoTxt.setText("");
		cargaHorariaSemanalCb.setItemSelected(0, true);
		remuneracaoCb.setText("");
		titularidadeCb.setItemSelected(0, true);
		atribuicoesTxt.setText("");
		cidadeTxt.setText("");
		estadosCb.setItemSelected(0, true);
		experienciaTxt.setText("");
		requisitosTxt.setText("");
	}
}